package com.lagou.registry;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Rpc服务提供者节点
 */
public class RpcProviderNode extends ServerNode{


    //当前app下注册的所有类名
    private Set<String> className;
    //key:className,value:methodName对应关系
    private Map<String,Set<String>> classMethodNameMap;

    public Set<String> getClassName() {
        return className;
    }

    public void setClassName(Set<String> className) {
        this.className = className;
    }

    public Map<String, Set<String>> getClassMethodNameMap() {
        return classMethodNameMap;
    }

    public void setClassMethodNameMap(Map<String, Set<String>> classMethodNameMap) {
        this.classMethodNameMap = classMethodNameMap;
    }

    @Override
    public String toString() {
        return "RpcProviderNode{" +
                "className=" + className +
                ", classMethodNameMap=" + classMethodNameMap +
                ", ip='" + ip + '\'' +
                ", port=" + port +
                ", appName='" + appName + '\'' +
                '}';
    }

    //构建服务器节点
    public static RpcProviderNode build(String ip,int port,String app,Set<Class> clazzs){
        RpcProviderNode node = new RpcProviderNode();
        node.setIp(ip);
        node.setPort(port);
        node.setAppName(app);
        Set<String> clazzNameSet = new HashSet<>();
        Map<String,Set<String>> map = new HashMap<>();
        for (Class clazz : clazzs) {
            String clazzName = clazz.getName();
            clazzNameSet.add(clazzName);
            Set<String> methodSet = new HashSet<>();
            for (Method method : clazz.getMethods()) {
                methodSet.add(method.getName());
            }
            map.put(clazzName,methodSet);
        }
        node.setClassName(clazzNameSet);
        node.setClassMethodNameMap(map);
        return node;
    }
}
